var net = require('net');
var server = net.createServer();

server.listen(8085, function () { 
  console.log('Listening on port 8085'); 
});

var conns = [];
server.on('connection', function (stream) {
  console.log('Accepting connection from', stream.remoteAddress);

  conns.push(stream);

  stream.on( 'data', recv.bind( null, stream ) );
  //读取数据
  // stream.on('data', function (data) { 
  //   recv.bind(stream,data);
  //   //stream.write(data); 
  //   // if(conns[0]){
  //     // conns[0].write(data)
  //   // }
  // });

  //关闭了
  stream.on('end', function (data) { 
    console.log('Connection closed'); 
  });
  


});

  //错误
server.on('error',function(err){
    console.log("error");
    server.close();
})

//链接和数据
function recv( socket, data ) {
  if ( data === 'quit' ) {
    socket.end( 'Bye!\n' );
    return;
  }

  console.log('DATA ' + socket.remoteAddress + ': ' + data);
}